#include "stdafx.h"
#include "trie.h"

#define CHAR_COUNT		(26 + 26 + 10 + 2)

struct TRIE_NODE
{
	int			nPatternID;
	TRIE_NODE*	pNodeList[CHAR_COUNT];
};

struct TRIE
{
	TRIE_NODE*	pRoot;
	int			nPatternCount;
};

TRIE* trie_create()
{
	TRIE* pResult = NULL;

	pResult = new TRIE;
	LOG_PROCESS_ERROR(pResult);

Exit0:
	return pResult;
}

bool trie_insert_pattern(TRIE* pTrie, const char* pcszPattern, int nPatternID)
{
	LOG_PROCESS_ERROR(pTrie);
	LOG_PROCESS_ERROR(pcszPattern);

	TRIE_NODE* pNode = pTrie->pRoot;
	while (*pcszPattern)
	{
		int nChar = CHAR_COUNT - 1;
		if (*pcszPattern >= 'a' && *pcszPattern <= 'z')
			nChar = *pcszPattern - 'a';
		else if (*pcszPattern >= 'A' && *pcszPattern <= 'Z')
			nChar = *pcszPattern - 'A' + 26;
		else if (*pcszPattern >= '0' && *pcszPattern <= '1')
			nChar = *pcszPattern - 'A' + 52;
		else if (*pcszPattern == '*')
			nChar = CHAR_COUNT - 2;

		if (pNode->pNodeList[nChar])
		{
			pNode = pNode->pNodeList[nChar];
		}
		else 
		{
			pNode->pNodeList[nChar] = new TRIE_NODE();
			pNode->pNodeList[nChar]->nPatternID = -1;
			pNode = pNode->pNodeList[nChar];
		}

		pcszPattern++;
	}

	if(pNode->nPatternID == -1)
	{
		pNode->nPatternID = nPatternID;
	}
	else
	{
		pNode->nPatternID = nPatternID;
	}

	return true;
Exit0:

	return false;
}

TRIE_RESULT* trie_search(TRIE* pTrie, const char* pcszKey)
{
	return NULL;
}

int trie_get_result_count(TRIE_RESULT* pTrieResult)
{
	return 0;
}

int trie_get_result_pattern(int nIndex)
{
	return 0;
}
